//
// Created by 相开原 on 2022/3/23.
//

#include "../headers/account.h"

void userManager() {
    while (1) {
        pointer *target;
        int choice, counter = 0;
        print_menu("[1]查询或修改用户信息\t[2]添加新用户\t[3]返回上一级");
        while (!input_int(&choice, 1, 3));
        switch (choice) {
            case 1:
                print_menu("[1]全部打印\t[2]根据用户名查找：");
                if (!input_int(&choice, 1, 2))
                    break;
                pointer *p = pointer_create();
                if (choice == 1)
                    for (account *i = head_user->next; i != NULL; i = i->next) {
                        pointer_add(p, i, 3, i->money);
                        counter++;
                    }
                if (choice == 2) {
                    char username[64];
                    printf("请输入用户名：");
                    while (!input_string(username, 64));
                    for (account *i = head_user->next; i != NULL; i = i->next)
                        if (strcmp(username, i->username) == 0) {
                            pointer_add(p, i, 3, i->money);
                            counter++;
                        }
                }
                if (p->next == NULL) {
                    puts("⚠️没有找到相关用户!");
                    return;
                }
                print_user(p);
                printf("请选择您要修改的用户序号（Enter返回）：");
                if (!input_int(&choice, 1, counter))
                    break;
                target = p;
                while (counter--)
                    target = target->next;
                user_modify(target->p_account);
                break;
            case 2:
                puts("您要创建的用户类型？");
                print_menu("[1]销售员\t[2]理货员\t[3]取消创建");
                while (!input_int(&choice, 1, 3));
                if (choice == 1) while (!user_register('X'));
                if (choice == 2) while (!user_register('L'));
                break;
            case 3:
                return;
        }
    }
}

// G 管理员, X 销售员, L 理货员
bool user_register(char mode) {
    account user;
    user.type = mode;

    printf("⏳ 正在建立%s账号，请牢记用户名与密码\n\n", get_userType(&user));

    char rePassword[64];
    printf("用户名：");
    while (!input_string(user.username, 64));
    printf("密码：");
    while (!input_string(user.password, 64));
    printf("重复密码：");
    while (!input_string(rePassword, 64));

    account *p = account_find(head_user, user.username);
    if (p != NULL) {
        puts("⚠️ 用户名已存在！");
        return false;
    }
    if (strcmp(user.password, rePassword) != 0) {
        puts("⚠️ 两次密码输入不一致!");
        return false;
    } else {
        get_MD5(user.password);
        get_timeItem(&user.addTime);
        user.money = 0;
        account_add(head_user, user);
        puts("✅ 账号建立成功！");
        return true;
    }
}

account *user_login() {
    account user;
    printf("用户名：");
    while (!input_string(user.username, 64));
    printf("密码：");
    while (!input_string(user.password, 64));
    get_MD5(user.password);

    account *p = account_find(head_user, user.username);
    if (p == NULL) {
        puts("⚠️ 用户名不存在！");
        return NULL;
    } else if (strcmp(p->password, user.password) != 0) {
        puts("⚠️ 用户名或密码错误！");
        return NULL;
    } else {
        printf("✅ 登录成功！尊敬的%s%s，欢迎回来！\n", get_userType(p), p->username);
        return p;
    }
}

account *account_find(account *head, char *username) {
    account *p = head;
    while (p->next != NULL) {
        p = p->next;
        if (strcmp(p->username, username) == 0) {
            return p;
        }
    }
    return NULL;
}

char *get_userType(account *p) {
    switch (p->type) {
        case 'G':
            return "管理员";
        case 'X':
            return "销售员";
        case 'L':
            return "理货员";
    }
    return "未知";
}

void user_modify(account *target) {
    print_menu("[1]修改用户名\t[2]修改密码\t[3]更改用户类型\t[4]删除用户\t[5]返回上一级");
    int choice;
    while (!input_int(&choice, 1, 5));
    puts("开始修改……按Enter可退出");
    switch (choice) {
        case 1:
            printf("请输入新的用户名：");
            if (input_string(target->username, 64))
                puts("✅ 修改成功");
            break;
        case 2:
            printf("请输入新的密码：");
            if (input_string(target->password, 64)) {
                get_MD5(target->password);
                puts("✅ 修改成功");
            }
            break;
        case 3:
            if (target->type == 'G') {
                puts("⚠️ 管理员账号不可修改类型！");
                break;
            }
            puts("请输入新的用户类型：");
            print_menu("[1]销售员\t[2]理货员\t[3]取消修改");
            while (!input_int(&choice, 1, 3));
            if (choice == 1) while (!user_register('X'));
            if (choice == 2) while (!user_register('L'));
            break;
        case 4:
            if (target->type == 'G') {
                puts("⚠️ 管理员账号不可删除！");
                break;
            }
            account_del(head_user, target);
            puts("✅ 删除成功");
            break;
        case 5:
            return;
    }
}
